package com.houtai.service.dict;


import com.houtai.bean.vo.dict.DictList;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.bean.vo.dict.DictResult;
import com.houtai.common.util.CommonUtil;
import com.houtai.common.util.Ip;
import com.houtai.dao.TsJzDictDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class DictService {
    private static Logger logger = Logger.getLogger(DictService.class);
    @Autowired
    private TsJzDictDao tsJzDictDao;

    @Autowired
    private RedisTemplate redisTemplate;

    public DictListResult findDictByType(String code_type) {
        String logInfo = ":findDictByType:";
        logger.info("======" + logInfo + "begin======");
        DictListResult retInfo = new DictListResult();
        try {

            List<DictList> dictLists = tsJzDictDao.findDictByType(code_type);
            if (dictLists != null && dictLists.size() > 0) {
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setDicts(dictLists);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无记录");
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public DictListResult findDictByParentId(int parent_id) {
        String logInfo = ":findDictByParentId:";
        logger.info("======" + logInfo + "begin======");
        DictListResult retInfo = new DictListResult();
        try {

            List<DictList> dictLists = tsJzDictDao.findDictByParentId(parent_id);
            if (dictLists != null && dictLists.size() > 0) {
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setDicts(dictLists);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无记录");
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public DictResult findDictByIp(HttpServletRequest request) {
        String logInfo = ":findDictByIp:";
        logger.info("======" + logInfo + "begin======");
        DictResult retInfo = new DictResult();
        try {

            DictList dictList = tsJzDictDao.findDictByName(CommonUtil.getCity(Ip.getIpAddr(request)));

            if (dictList != null) {
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setDict(dictList);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无记录");
            }
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}

